package dog.weatherwallpaper.model;

import org.json.JSONObject;

import dog.weatherwallpaper.R;
import dog.weatherwallpaper.utils.App;
import dog.weatherwallpaper.utils.Time;

public final class FutureWeather extends AbstractWeather {
	/**
	 * Local day, NOT UTC. Format yyyy-MM-dd
	 */
	private String date = App.context().getString(R.string.weekday);
	@SuppressWarnings("unused")
	private String winddirection = "";
	private int tempMaxC;
	private int tempMaxF;
	private int tempMinC;
	private int tempMinF;

	FutureWeather(final JSONObject json) {
		super(json);
		if (json == null) return;
		this.date = json.optString("date");
		this.tempMaxC = json.optInt("tempMaxC");
		this.tempMaxF = json.optInt("tempMaxF");
		this.tempMinC = json.optInt("tempMinC");
		this.tempMinF = json.optInt("tempMinF");
		this.winddirection = json.optString("winddirection");
	}

	@Override
	public String getBasicInfo(Unit unit) {
		StringBuilder sb = new StringBuilder();
		sb.append(this.getDay(false));
		sb.append(SPACE);
		sb.append(this.temperature(unit.isCelsius()));
		if (precipMM != 0) {
			sb.append(SPACE);
			sb.append(super.getPrecipitation());
		}
		sb.append(SPACE);
		sb.append(super.getWeatherDesc());
		return sb.toString();
	}

	@Override
	public String getAllInfo(Unit unit) {
		StringBuilder sb = new StringBuilder();
		sb.append(createLine(R.string.date, this.getDay(true)));
		sb.append(createLine(R.string.temperature, this.temperature(unit.isCelsius())));
		sb.append(createLine(R.string.description, super.getWeatherDesc()));
		sb.append(createLine(R.string.wind, super.getWindSpeed(unit.isKm()) + super.getWindDirection()));
		if (precipMM != 0) {
			sb.append(createLine(R.string.precipitation, super.getPrecipitation()));
		}
		return sb.toString();
	}

	String getDate() {
		return date;
	}

	private String temperature(final boolean celsius) {
		if (celsius) {
			final String C = App.context().getString(R.string.c);
			return tempMinC + C + PIPE + tempMaxC + C;
		} else {
			final String F = App.context().getString(R.string.f);
			return tempMinF + F + PIPE + tempMaxF + F;
		}
	}

	private String getDay(final boolean full) {
		Time t = new Time();
		if (date.equals(t.today())) {
			return App.context().getString(R.string.today);
		} else {
			return t.formatDate(date, full);
		}
	}

	public int getTempMinC() {
		return tempMinC;
	}

	public int getTempMaxC() {
		return tempMaxC;
	}

	public int getTempMinF() {
		return tempMinF;
	}

	public int getTempMaxF() {
		return tempMaxF;
	}
}